/*
 * This file is part of TradeHelper library.
 *
 * TradeHelper library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TradeHelper library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TradeHelper library.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.rag.forum.pages;

import com.alee.extended.filechooser.WebFilePlate;
import com.alee.extended.image.WebImage;
import com.alee.extended.layout.WrapFlowLayout;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.scroll.WebScrollBar;
import com.alee.laf.scroll.WebScrollPane;
import com.alee.managers.settings.SettingsManager;
import com.alee.managers.tooltip.TooltipManager;
import com.alee.managers.tooltip.TooltipWay;
import com.alee.utils.swing.AncestorAdapter;
import com.rag.core.Item;
import com.rag.forum.ui.ItemField;
import com.rag.forum.ui.ItemListener;
import com.rag.forum.ui.ItemPopup;

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * User: mgarin Date: 19.04.12 Time: 19:23
 */

public class DatabasePage extends WebPanel
{
    private static ImageIcon closeIcon = WebFilePlate.CROSS_ICON;

    private ItemField searchField;
    private WebPanel resultsPanel;
    private WebScrollPane resultsScroll;

    public DatabasePage ()
    {
        super ();
        setMargin ( 5 );

        searchField = new ItemField ();
        searchField.setShadeWidth ( 5 );
        searchField.addItemListener ( new ItemListener ()
        {
            public void itemSelected ( Item item )
            {
                searchField.clear ();

                final WebPanel itemPanel = new WebPanel ( true )
                {
                    protected void paintComponent ( Graphics g )
                    {
                        super.paintComponent ( g );

                        Graphics2D g2d = ( Graphics2D ) g;
                        Rectangle cr = getCloseIconRect ( this );
                        g2d.drawImage ( closeIcon.getImage (), cr.x, cr.y, null );
                    }

                    public Dimension getPreferredSize ()
                    {
                        Dimension ps = super.getPreferredSize ();
                        WebScrollBar vsb = resultsScroll.getVerticalScrollBar ();
                        ps.width = Math.min ( ps.width, resultsScroll.getWidth () -
                                ( vsb.isShowing () ? vsb.getWidth () : 0 ) );
                        return ps;
                    }
                };
                itemPanel.setShadeWidth ( 5 );
                itemPanel.setMargin ( 10 );
                itemPanel.add ( ItemPopup.createItemDescription ( item ), BorderLayout.CENTER );
                itemPanel.addMouseListener ( new MouseAdapter ()
                {
                    public void mousePressed ( MouseEvent e )
                    {
                        if ( SwingUtilities.isMiddleMouseButton ( e ) )
                        {
                            closePanel ();
                        }
                        else if ( getCloseIconRect ( itemPanel ).contains ( e.getPoint () ) )
                        {
                            closePanel ();
                        }
                    }

                    private void closePanel ()
                    {
                        resultsPanel.remove ( itemPanel );
                        resultsPanel.revalidate ();
                        resultsPanel.repaint ();
                    }
                } );
                resultsPanel.add ( itemPanel, 0 );
            }
        } );
        add ( searchField, BorderLayout.NORTH );

        final WebImage image = new WebImage ( DatabasePage.class, "icons/database/search.png" );
        final String searchTip = "Введите любой текст для поиска итемов по названию или описанию";
        TooltipManager.setTooltip ( image, searchTip, TooltipWay.down );
        searchField.setLeadingComponent ( image );

        WrapFlowLayout wrapFlowLayout = new WrapFlowLayout ( true );
        wrapFlowLayout.setWrapEachComponent ( true );
        resultsPanel = new WebPanel ( wrapFlowLayout );
        resultsScroll = new WebScrollPane ( resultsPanel, false, false );
        add ( resultsScroll, BorderLayout.CENTER );

        addAncestorListener ( new AncestorAdapter ()
        {
            public void ancestorAdded ( AncestorEvent event )
            {
                if ( !SettingsManager.getBoolean ( "DatabasePage.shownOnce", false ) )
                {
                    SettingsManager.set ( "DatabasePage.shownOnce", true );
                    TooltipManager.showOneTimeTooltip ( image, null, searchTip, TooltipWay.down );
                }
            }
        } );
    }

    private static Rectangle getCloseIconRect ( WebPanel panel )
    {
        return new Rectangle (
                panel.getWidth () - panel.getShadeWidth () - 2 - closeIcon.getIconWidth (),
                panel.getShadeWidth () + 2, closeIcon.getIconWidth (), closeIcon.getIconHeight () );
    }
}